#!/bin/bash
########################################
# basic top and memory stats logging for
# siege and apachebench benchmarks of centminmod.com
########################################
SCRIPTNAME='logstats.sh'
SCRIPTVER='v0.0.3'
AUTHOR='by George Liu (eva2000)'
AUTHORURL='http://vbtechsupport.com'
########################################

# Number of seconds to gather stats
# i.e. if siege bench runs for 300 seconds set it to 300
SECONDS='60'

# Number of lines to return from top stats output
# Will vary depending on tops output, number of cpus 
# displayed. Top find the number of lines type:
# top -b -n1
# count how many lines to include Mem: output and 
# set that value for LINES variable
LINES='4'

# What directory and file name for log file
LOGDIR='/root/siege'
LOGFILENAME='topstats.txt'
LOGFILE="${LOGDIR}/${LOGFILENAME}"

if [ ! -d $LOGDIR ]; then
mkdir $LOGDIR
fi

function funct_logstats {

echo "----------------------------------------------------"
echo "$SCRIPTNAME $SCRIPTVER"
echo "$AUTHOR - $AUTHORURL"
echo "----------------------------------------------------"
echo "starting cpu & memory logging... run for $SECONDS seconds."


for i in `seq 1 $SECONDS` 
    do 
	echo "---------------------------------------------------------------------------" >> $LOGFILE
	top -b -n1 | head -n$LINES >> $LOGFILE
	echo "---------------------------------------------------------------------------" >> $LOGFILE
	free -m >> $LOGFILE
	echo "---------------------------------------------------------------------------" >> $LOGFILE
	echo -e "\n" >> $LOGFILE 
	usleep 500000
   done

echo ""
echo "----------------------------------------------------"
echo "cpu & memory logging finished..."
echo "log file saved at $LOGFILE"
echo "----------------------------------------------------"

}

function funct_cleanup {

echo "----------------------------------------------------"
echo "remove $LOGFILE"
echo "checking $LOGDIR"
echo "----------------------------------------------------"

ls -lht $LOGDIR

echo

if [ -f $LOGFILE ]; then

rm -rf $LOGFILE

echo "----------------------------------------------------"
echo "$LOGFILE removed"
echo "----------------------------------------------------"

else

echo "----------------------------------------------------"
echo "$LOGFILE doesn't exist"
echo "----------------------------------------------------"

fi

}

function funct_filtertop {

echo "----------------------------------------------------"
echo "$SCRIPTNAME $SCRIPTVER"
echo "$AUTHOR - $AUTHORURL"
echo "----------------------------------------------------"
echo "Filtering cpu load stats from $LOGFILE"
echo "----------------------------------------------------"
grep ^top $LOGFILE
echo "----------------------------------------------------"

}

function funct_filtermem {

echo "----------------------------------------------------"
echo "$SCRIPTNAME $SCRIPTVER"
echo "$AUTHOR - $AUTHORURL"
echo "----------------------------------------------------"
echo "Filtering memory stats from $LOGFILE"
echo "----------------------------------------------------"
grep -E '(^top|^Mem:)' $LOGFILE
echo "----------------------------------------------------"

}

############################

case "$1" in
log)

funct_logstats

;;
cpuload)

funct_filtertop

;;
memstats)

funct_filtermem

;;
cleanup)

funct_cleanup

;;
exit)

echo ""
echo "exit"
exit 0

;;
*)

echo "$0 log"
echo "$0 cpuload"
echo "$0 memstats"
echo "$0 cleanup"

;;
esac

exit